package com.mao.interview.permutation;

import java.util.ArrayList;

/**
 * 重复数字的全排列问题，求出数字{1,2,2,3,5}的所有排列，重复的不算
 *
 */
public class RepeatNumberDemo {

	public static void main(String[] args) {
		char[] basicNums = new char[] { '1', '2', '3', '5' };

		ArrayList<char[]> list = new ArrayList<>();
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (j == i) {
					continue;
				}
				for (int x = 0; x < 4; x++) {
					if (x == i || x == j) {
						continue;
					}
					for (int y = 0; y < 4; y++) {
						if (y == i || y == j || y == x) {
							continue;
						}
						StringBuilder sb = new StringBuilder();
						sb.append(basicNums[i]);
						sb.append(basicNums[j]);
						sb.append(basicNums[x]);
						sb.append(basicNums[y]);
						System.out.println(sb.toString());
						list.add(sb.toString().toCharArray());
						sb = null;
					}
				}
			}
		}

		for (char[] charArray : list) {
			insertChar('2', charArray);
		}
	}

	private static void insertChar(char ch, char[] array) {
		int size = array.length;
		ArrayList<Character> outSequence = new ArrayList<>(size + 1);
		for (int i = 0; i <= size; i++) {
			for (int j = 0; j < size; j++) {
				outSequence.add(array[j]);
			}
			boolean shouldJump = false;
			if (i < size && ch == array[i]) {
				shouldJump = true;
			}
			outSequence.add(i, ch);
			if (shouldJump) {
				i++;
			}
			System.out.println(outSequence.toString());
			outSequence.clear();
		}
	}

}
